package com.wandian.cqcsk.mvvm.main;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;

import com.alibaba.android.arouter.launcher.ARouter;
import com.mylib.libcore.AppContext;
import com.mylib.libcore.bean.UserInfoBean;
import com.mylib.libcore.config.RouterConfig;
import com.mylib.libcore.utils.TabKeyUtils;
import com.mylib.libcore.utils.UIUtils;
import com.yilan.sdk.data.user.YLUser;
import com.yilan.sdk.ui.configs.CommentConfig;
import com.yilan.sdk.ui.configs.YLUIConfig;
import com.yilan.sdk.ui.little.YLLittleVideoFragment;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.ArrayList;

import static com.mylib.libcore.config.AppSPKeyConfig.APP_FUNCTION_TABS;
import static com.mylib.libcore.config.AppSPKeyConfig.HOME_TAB_JSON;


public class HomeFragmentAdapter extends FragmentStatePagerAdapter {

    private ArrayList<Fragment> fragmentList = new ArrayList<>();

    public HomeFragmentAdapter(@NonNull FragmentManager fm, int behavior) {
        super(fm, behavior);
        fragmentList.clear();
        String tab_json = AppContext.getInstance().getAppPreferenceForever().getSelectTabs();//设置功能
        String tab_opens = AppContext.getInstance().getAppPreferenceForever().getMainTabs()+"";//开放
        if(!UIUtils.isEmpty(tab_json)){
            try {
                JSONArray jsonArray = new JSONArray(tab_json);
                if(!UIUtils.isEmpty(jsonArray) && jsonArray.length()>0){
                    for(int i=0;i<jsonArray.length();i++){
                        String key = jsonArray.getString(i);
                        if(TabKeyUtils.checkTabCanUse(key)){//没限制直接添加
                            addTable(key+"");
                        } else{
                            if(tab_opens.contains(key))addTable(key+"");
                        }
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
                if(tab_opens.contains("天气")) fragmentList.add((Fragment) ARouter.getInstance().build(RouterConfig.ROUTER_FRAGMENT_WEATHER_HOME).navigation());//天气1
                fragmentList.add((Fragment) ARouter.getInstance().build(RouterConfig.ROUTER_FRAGMENT_NEWS_LIFE_TOOL).navigation());//生活9

                fragmentList.add((Fragment) ARouter.getInstance().build(RouterConfig.ROUTER_FRAGMENT_NEWS_KNOWLEDGE).navigation());//智库10
            }

        }else {
            if(tab_opens.contains("天气")) fragmentList.add((Fragment) ARouter.getInstance().build(RouterConfig.ROUTER_FRAGMENT_WEATHER_HOME).navigation());//天气1

            fragmentList.add((Fragment) ARouter.getInstance().build(RouterConfig.ROUTER_FRAGMENT_NEWS_LIFE_TOOL).navigation());//生活9

            fragmentList.add((Fragment) ARouter.getInstance().build(RouterConfig.ROUTER_FRAGMENT_NEWS_KNOWLEDGE).navigation());//智库10
        }

        fragmentList.add((Fragment) ARouter.getInstance().build(RouterConfig.ROUTER_HOME_FRAGMENT_MINE).navigation());//我的5
    }


    private void addTable(String tabTitle){
        switch (tabTitle){
            case "天气":
                fragmentList.add((Fragment) ARouter.getInstance().build(RouterConfig.ROUTER_FRAGMENT_WEATHER_HOME).navigation());//天气1
                break;
            case "资讯":
                fragmentList.add((Fragment) ARouter.getInstance().build(RouterConfig.ROUTER_FRAGMENT_NEWS_TOUTIAO).navigation());//新闻2
                break;
            case "游戏":
                fragmentList.add((Fragment) ARouter.getInstance().build(RouterConfig.ROUTER_FRAGMENT_GAMES_ON_LINE).navigation());//游戏
                break;
            case "玩赚":
                fragmentList.add((Fragment) ARouter.getInstance().build(RouterConfig.ROUTER_FRAGMENT_GAMES_HOME).navigation());//玩赚
                break;
            case "音乐":
                fragmentList.add((Fragment) ARouter.getInstance().build(RouterConfig.ROUTER_FRAGMENT_AUDIO_HOME).navigation());//音乐4
                break;
            case "小说":
                fragmentList.add((Fragment) ARouter.getInstance().build(RouterConfig.ROUTER_FRAGMENT_GAMES_BOOK).navigation());//小说6
                break;
            case "视频":
                //设置用户信息
                //建议放在小视频页面展示之前调用，例如在Splash页面或MainActivity中调用
                YLLittleVideoFragment.preloadVideo();
                UserInfoBean userInfoBean = AppContext.getInstance().getAppPreferencesHelper().getUserInfoBean();
                YLUser.getInstance().login(userInfoBean.getNickName()+"", userInfoBean.getHeadPic()+"", UIUtils.getRandPhoneNum()+"", userInfoBean.getId()+"");
                // 页面UI配置
                YLUIConfig.getInstance()
                        .littleLikeShow(true)//是否显示小视频点赞
                        .littleShareShow(true)//是否显示小视频分享
                        .littleShowGuide(true)//是否显示新手引导滑动浮层（第一次安装进入显示）
                        .littleComment(CommentConfig.CommentType.SHOW_COMMENT_ALL)//小视频评论展现形式
                        .videoComment(CommentConfig.CommentType.SHOW_COMMENT_ALL)//短视频评论展现形式
                        .videoLikeShow(true)//是否显示短视频点赞
                        .videoShareShow(true)//是否显示短视频分享
                        .followAvailable(true)//是否显示关注按钮
                        .followChannelAvailable(true)//是否在频道页面展示关注
                        .feedAvatarClickable(true)//短视频头像是否可以点击
                        .setLittleTitleBottom(0)//设置小视频标题距离底部的间距，单位dp
                        .feedPlayAuto(true);//feed自动播放（仅在FeedConfig.getInstance().setPlayerStyle(FeedConfig.STYLE_FEED_PLAY) 时有效）
                fragmentList.add( YLLittleVideoFragment.newInstance());
               // fragmentList.add((Fragment) ARouter.getInstance().build(RouterConfig.ROUTER_FRAGMENT_VIDEO_YLSP).navigation());//视频7
                break;
            case "微文":
                fragmentList.add((Fragment) ARouter.getInstance().build(RouterConfig.ROUTER_FRAGMENT_NEWS_WX_TOP_PAGE).navigation());//微文8
                break;
            case "生活":
                fragmentList.add((Fragment) ARouter.getInstance().build(RouterConfig.ROUTER_FRAGMENT_NEWS_LIFE_TOOL).navigation());//生活9
                break;
            case "智库":
                fragmentList.add((Fragment) ARouter.getInstance().build(RouterConfig.ROUTER_FRAGMENT_NEWS_KNOWLEDGE).navigation());//智库10
                break;
        }

    }

    @Override
    public Fragment getItem(int position) {
        if(position>=fragmentList.size())return new Fragment();
        return  fragmentList.get(position);
    }

    @Override
    public int getCount() {
        return fragmentList.size();
    }


}
